package host.skiree;

import java.util.HashMap;
import java.util.Map;

import java.util.HashMap;
import java.util.Map;

public class CronExpressionToChinese {

    private static final Map<String, String> translations = new HashMap<>();

    static {
        translations.put("*", "每");
        translations.put("?", "不确定");
        translations.put("L", "最后一天");
        translations.put("W", "工作日最近的工作日");
        translations.put("C", "本月最近的工作日");
        translations.put("0", "星期日");
        translations.put("1", "星期一");
        translations.put("2", "星期二");
        translations.put("3", "星期三");
        translations.put("4", "星期四");
        translations.put("5", "星期五");
        translations.put("6", "星期六");
    }

    public static String convertToChinese(String cronExpression) {
        String[] fields = cronExpression.split(" ");
        StringBuilder sb = new StringBuilder();

        sb.append("秒：").append(convertFieldToChinese(fields[0])).append("\n");
        sb.append("分：").append(convertFieldToChinese(fields[1])).append("\n");
        sb.append("小时：").append(convertFieldToChinese(fields[2])).append("\n");
        sb.append("日期：").append(convertFieldToChinese(fields[3])).append("\n");
        sb.append("月份：").append(convertFieldToChinese(fields[4])).append("\n");
        sb.append("星期：").append(convertFieldToChinese(fields[5])).append("\n");
        sb.append("年份：").append(convertFieldToChinese(fields[6]));

        return sb.toString();
    }

    private static String convertFieldToChinese(String field) {
        StringBuilder sb = new StringBuilder();

        if (field.equals("*")) {
            sb.append("每");
        } else if (field.contains("/")) {
            String[] parts = field.split("/");
            sb.append(translations.get(parts[1])).append("开始，每").append(parts[0]).append("个");
        } else if (field.contains("-")) {
            String[] parts = field.split("-");
            sb.append(translations.get(parts[0])).append("到").append(translations.get(parts[1]));
        } else if (field.contains(",")) {
            String[] parts = field.split(",");
            for (String part : parts) {
                sb.append(translations.get(part)).append("、");
            }
            sb.deleteCharAt(sb.length() - 1);
        } else if (field.contains("L")) {
            sb.append(translations.get("L"));
        } else if (field.contains("W")) {
            sb.append(translations.get("W"));
        } else if (field.contains("C")) {
            sb.append(translations.get("C"));
        } else {
            sb.append(translations.get(field));
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String cronExpression = "0 0 0 L-3 * ? *";
        String chineseDescription = CronExpressionToChinese.convertToChinese(cronExpression);
        System.out.println(chineseDescription);
    }
}

